define(function () {
    return function (pane) {
        /**
         * 导航
         */
        {
            pane.html(utils.getHtml('default_table'));

            utils.addFilterGroupbtn(pane, {
                text: '全部',
                name: 'type',
                filters: dicts.suggestions_type
            });

            utils.addFilterGroupbtn(pane, {
                name: 'status',
                filters: dicts.status
            });
        }

        //全局变量
        var fields_arrays = [
            ['type', 'select', '类别', false, dicts.suggestions_type],
            ['content', 'textarea', '内容'],
            ['tel', 'text', '电话'],
            ['qq', 'text', 'QQ'],
            ['feature', 'select', '推荐', false, dicts.boolean],
            ['order_by', 'text', '排序'],
            ['status', 'select', '状态', false, dicts.status]
        ];
        var addFields = utils.convertFields(fields_arrays),
            editFields = utils.convertFields(fields_arrays);

        //初始化工具
        utils.addTools(pane, [
            {className: 'add-suggestion-btn', icon: 'fa-plus', text: '添加建议反馈'},
            {className: 'refresh-btn', icon: 'fa-refresh', text: '刷新'}]
        );
        $(pane).on('click', '.add-suggestion-btn', function () {
            utils.formAdd(table, {
                name: '建议反馈', fields: addFields, columns: 6,
                fnServerData: function (paramData) {
                    return adminApi('suggestions_add', paramData);
                }
            });
        }).on('click', '.refresh-btn', function () {
            if (table) {
                table.api().draw();
                analyse();
            }
        });

        //初始化表格
        var table = utils.dataTable(pane, {
            columns_arrays: [
                ["id", "#"],
                ["user_id", "用户", false, function (i, t, row) {
                    var user = row.user;
                    if (!user) {
                        return '';
                    }
                    return (user.image ? '<img src="' + user.image + '?imageView2/1/w/150/h/150/q/75"> ' : '') + '<div>' + (user.nickname || user.tel) + '</div>';
                }, 'filterable'],
                ["type", "类别", false, utils.dictRender('suggestions_type')],
                ["content", "内容", false],
                ["tel", "电话", false],
                ["qq", "QQ", false],
                ["status", "状态", false, utils.dictRender('status')],
                ["created_at", "创建时间", false, utils.dateTimeRender()]
            ],
            sorting: [[0, 'desc']],
            fnServerData: function (paramData) {
                return adminApi('suggestions_browse', paramData);
            }
        });

        //编辑开关
        var toggleHandler = utils.toggleChild(pane, 0, function (checked) {
            table.api().rows().every(function (idx) {
                var child = this.child, data = this.data();
                if (checked == 1) {
                    var opts = [
                        {text: '编辑建议反馈', icon: 'fa-edit', className: 'edit-ope'},
                        {text: '删除建议反馈', icon: 'fa-trash-o', className: 'delete-ope btn-danger'},
                        {text: '发送消息', className: 'send-msg-opt'}
                    ];
                    child(utils.childWrapper(idx, opts)).show();
                } else {
                    child.hide();
                }
            });
        });

        //初始化事件
        table.on('draw.dt', function () {
            toggleHandler();
        }).on('click', '.edit-ope', utils.tableEvent(table, function (idx, rowData) {
            //编辑
            adminApi('suggestions_load', {id: rowData.id}).then(function (data) {
                utils.formEdit(table, {
                    idx: idx, name: '', fields: editFields, columns: 6, data: data,
                    fnServerData: function (paramData) {
                        return adminApi('suggestions_edit', paramData);
                    }
                });
            });
        })).on('click', '.delete-ope', utils.tableEvent(table, function (idx, rowData) {
            //删除
            utils.confirmation(this, {
                placement: 'top',
                optName: '删除建议反馈',
                title: '你确定删除建议反馈?',
                fnConfirm: function () {
                    return adminApi('suggestions_delete', {id: rowData.id}).then(utils.handDelete(table, {name: '建议反馈'}));
                }
            });
        })).on('click', '.send-msg-opt', utils.tableEvent(table, function (idx, rowData) {
            //发送消息
            utils.confirmation(this, {
                placement: 'top',
                optName: '发送消息',
                title: '发送消息',
                fnConfirm: function () {
                    return adminApi('users_send_msg', {
                        openid: rowData.user && rowData.user.openid || '',
                        text: $('#msgText').val()
                    });
                },
                template: '<div class="popover"> <div class="arrow"></div> <h3 class="popover-title">发送消息</h3>' +
                '<div class="popover-form"> <textarea id="msgText" class="form-control"></textarea> </div>' +
                '<div class="popover-content"> <a data-apply="confirmation">Yes</a> <a data-dismiss="confirmation" class="pull-right">No</a> </div> </div>'
            });
        }));
    }
});